package utl.mofen.script;


import java.util.Hashtable;

import utl.mofen.intf.IConfigLoader;

/**
 * 脚本对象中心
 * @author mofen
 *
 */
public class ScriptObjectCenter {	
	IConfigLoader loader; 
	private Boolean useCache = true;
	
	private Hashtable<String, ScriptObject> cache = new Hashtable<String, ScriptObject>();


	public void setUseCache(Boolean p_useCache)
	{
		this.useCache = p_useCache;
	}
	
	public boolean isInitailized()
	{
		return (loader!=null);
	}
	
	public void checkInitialize(IConfigLoader __loader)
	{
		if (__loader!=null)
		{
			setLoader(__loader);
		}
	}
	
	/**
	 * 获取一个脚本对象
	 * @param strIndex
	 * @param strVer
	 * @return
	 * @throws Exception
	 */
	public ScriptObject getScriptObject(String strIndex, String strVer) throws Exception
	{
		String key = strIndex.toLowerCase() + "_" + strVer.toLowerCase();
		ScriptObject obj = null;
		if (useCache)
		{
			obj = cache.get(key);
		}
		if (obj==null)
		{
			obj = new ScriptObject(strIndex, strVer);			
			obj.setDataScript(loader.getString(strIndex, strVer));	
			
			if (useCache)
			{
				cache.put(key, obj);
			}	
			
			//解析子脚本
			obj.parseSubItems(this, strVer);
		}
		return obj;
	}
	
	/**
	 * 解析一个脚本中的子脚本
	 * @param strIndex
	 * @param strVer
	 * @throws Exception
	 */
	public void parseSubItems(String strIndex, String strVer) throws Exception
	{
		ScriptObject obj = getScriptObject(strIndex, strVer);
		obj.parseSubItems(this, strVer);
	}

	
	/**
	 * 设置配置加载接口	
	 * @param scriptLoader
	 */
	public void setLoader(IConfigLoader scriptLoader) {
		this.loader = scriptLoader;
	}
}
